using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using student_factory.DAO;
using student_factory.Metier;

namespace student_factory.EnseignerMatiere {
    public partial class EnseignerMatiere : Form {

        private IList<Projet> myProjets = new List<Projet>();
        private IList<Etudiant> myEtudiants = new List<Etudiant>();
        private Etudiant selectedEtudiant = new Etudiant();
        private IList<Matiere> myMatieres = new List<Matiere>();

        public EnseignerMatiere()
        {
            InitializeComponent();
        }

        private void EnseignerMatiere_Load(object sender, EventArgs e)
        {
            myProjets = new List<Projet>();
            IList<Projet> projets = ProjetDAO.getActiveProjects();
            int i = 0;
            foreach(Projet projet in projets)
            {
                projets_comboBox.Items.Insert(i, projet.Prj_nom);
                myProjets.Insert(i, projet);
                i++;
            }
        }

        private void projets_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            myEtudiants = new List<Etudiant>();
            resetEtudiants();
            resetMatieres();
            Projet selectedProjet = myProjets[projets_comboBox.SelectedIndex];
            IList<Etudiant> etudiants = EtudiantDAO.getEtudiantsFromProject(selectedProjet);
            int i = 0;
            foreach (Etudiant etudiant in etudiants)
            {
                etudiants_comboBox.Items.Insert(i, etudiant.Etu_nom + " - " + etudiant.Etu_prenom);
                myEtudiants.Insert(i, etudiant);
                i++;
            }
        }

        private void etudiants_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            myMatieres = new List<Matiere>();
            resetMatieres();
            selectedEtudiant = myEtudiants[etudiants_comboBox.SelectedIndex];
            prenomEtu_textBox.Text = selectedEtudiant.Etu_prenom;
            nomEtu_textBox.Text = selectedEtudiant.Etu_nom;
            ddnEtu_textBox.Text = selectedEtudiant.Etu_date_naissance;
            chargeMatiere(selectedEtudiant);
        }

        private void resetEtudiants() {
            etudiants_comboBox.Items.Clear();
            etudiants_comboBox.Update();
            etudiants_comboBox.Text = "Choisissez un Etudiant";
            etudiants_comboBox.Update();
        }

        private void resetMatieres()
        {
            nomEtu_textBox.Clear();
            nomEtu_textBox.Update();
            prenomEtu_textBox.Clear();
            prenomEtu_textBox.Update();
            ddnEtu_textBox.Clear();
            ddnEtu_textBox.Update();
            mat_dataGridView.DataSource = new DataTable();
            mat_dataGridView.Update();
        }

        private void chargeMatiere(Etudiant etu)
        {
            DataTable leDataTable = new DataTable();

            DataColumn leDataColumn;
            DataRow leDataRow;

            leDataColumn = new DataColumn();
            leDataColumn.ColumnName = "Annee";
            leDataTable.Columns.Add(leDataColumn);
            leDataColumn.ReadOnly = true;
            leDataColumn = new DataColumn();
            leDataColumn.ColumnName = "Matiere";
            leDataTable.Columns.Add(leDataColumn);
            leDataColumn.ReadOnly = true;
            leDataColumn = new DataColumn();
            leDataColumn.ColumnName = "Confirmer ?";
            leDataColumn.ReadOnly = false;
            leDataTable.Columns.Add(leDataColumn);
            
            myMatieres = MatiereDAO.getMatieresFromEtudiant(etu);
            foreach (Matiere mat in myMatieres)
            {
                leDataRow = leDataTable.NewRow();
                leDataRow["Annee"] = MatiereDAO.getAnneeSuiviMatiere(etu, mat);
                leDataRow["Matiere"] = mat.Mat_description;
                leDataRow["Confirmer ?"] = MatiereDAO.isMatiereValide(etu, mat) ? "Oui" : "Non";
                leDataTable.Rows.Add(leDataRow);
            }

            mat_dataGridView.DataSource = leDataTable;
        }

        private void mat_dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string value = mat_dataGridView.CurrentCell.Value.ToString();
            if( value == "Oui" || value == "Non"){
                bool confirm = value == "Oui" ? true : false;
                MatiereDAO.setMatiereConfirmation(selectedEtudiant, myMatieres[e.RowIndex], confirm);
            }
        }

        private void Quitter_Click(object sender, EventArgs e)
        {
            Dispose();
        }

   }
}